﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TVideo.Util;
using TVideo.ViewModel;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;
using Wpf.Ui.Controls.Interfaces;
using Wpf.Ui.Extensions;
using Wpf.Ui.Mvvm.Contracts;
using Wpf.Ui.TaskBar;

namespace TVideo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        private bool _initialized = false;
        private readonly IThemeService _themeService;
        private readonly ITaskBarService _taskBarService;
        private readonly IPageService _pageService;

        public MainWindow(IThemeService themeService, ITaskBarService taskBarService, IPageService pageService)
        {
            _themeService = themeService;
            _taskBarService = taskBarService;
            _pageService = pageService;

            InitializeComponent();

            this.Loaded += MainWindow_Loaded;
            this.Closed += MainWindow_Closed;
        }

        private void MainWindow_Closed(object? sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            InitRootNavigation();
            InvokeSplashScreen();
        }

        private void InvokeSplashScreen()
        {
            if (_initialized)
                return;

            _initialized = true;

            RootMainGrid.Visibility = Visibility.Collapsed;
            RootWelcomeGrid.Visibility = Visibility.Visible;

            _taskBarService.SetState(this, TaskBarProgressState.Indeterminate);

            Task.Run(async () =>
            {
                // Remember to always include Delays and Sleeps in
                // your applications to be able to charge the client for optimizations later.
                await Task.Delay(2000);

                await Dispatcher.InvokeAsync(() =>
                {
                    RootWelcomeGrid.Visibility = Visibility.Hidden;
                    RootMainGrid.Visibility = Visibility.Visible;

                    //Navigate(typeof(Pages.Dashboard));

                    _taskBarService.SetState(this, TaskBarProgressState.None);
                });

                return true;
            });
        }

        private void InitRootNavigation()
        {
            //RootNavigation.PageService = _pageService;

            //RootNavigation.DataContextFun = (item) =>
            //{
            //    string pageTag = item.PageTag;
            //    string url = WebsiteUtil.GetUrl(pageTag);

            //    var vm = App.GetService<BrowserViewModel>();
            //    vm!.Url = url;
            //    vm!.Address = url;
            //    return vm;
            //};

            RootNavigation.Navigated += RootNavigation_Navigated;
        }

        private void RootNavigation_Navigated([System.Diagnostics.CodeAnalysis.NotNull] INavigation sender, Wpf.Ui.Common.RoutedNavigationEventArgs e)
        {
            string pageTag = e.CurrentPage.PageTag;
            string url = WebsiteUtil.GetUrl(pageTag);

            var vm = App.GetService<BrowserViewModel>();
            vm!.Url = url;
            vm!.Address = url;

            RootNavigation.SetContext(pageTag, vm);
        }

        private void NavigationItem_Click(object sender, RoutedEventArgs e)
        {
            var themeType = _themeService.GetTheme() == ThemeType.Dark ? ThemeType.Light : ThemeType.Dark;
            _themeService.SetTheme(themeType);
        }

        private void NavigationItemPlay_Click(object sender, RoutedEventArgs e)
        {
            string url = "";
            int index = RootNavigation.SelectedPageIndex;
            if (index > -1)
            {
                var vm = RootNavigation.GetContext<BrowserViewModel>(index);

                if (vm != null)
                {
                    url = vm.Address;
                }
            }

            if (string.IsNullOrEmpty(url))
            {

            }
            else
            {
                PlayWindow window = new PlayWindow(url);
                window.Show();
            }
        }
    }
}
